﻿function initDialog(id, height, width, callback, changeUrlCallback) {
    $("#" + id + "").dialog({
        autoOpen: false,
        resizable: true,
        height: height,
        width: width,
        resizable: true,
        buttons: {
            "OK": function () {
                if (callback != undefined) {
                    callback();
                }
                $(this).dialog('close');
                changeUrlCallback();
            },
            "Close": function () {
                if (changeUrlCallback) {
                    changeUrlCallback();
                }
                $(this).dialog('close');
            }
        }
    })
}

var HistoryHelper = function () {
    this.history = window.history;
    this.url = window.location.href;
    this.baseUrl = this.url.split('?')[0];

    this.update = function (state) {
        var url = this.baseUrl + "?";
        if (state != null) {
            for (var i in state) {
                url += i + "=" + state[i] + "&";
            }
            url = url.substring(0, url.length - 1);
        }
        window.history.pushState(state, "", url);
        this.currentHistoryState = state;
    }
    this.replaceLink = function (state, link) {
        this.history.replaceState(state, "", link);
    }

    this.getParameters = function () {
        var keys = [];
        var vals = [];
        if (window.location.search.indexOf('?') == -1) {
            return null;
        }
        var url = window.location.search.split('?')[1];
        if (url.split('&') == -1) {
            keys.push(url.split('=')[0]);
            vals.push(url.split('=')[1]);
        } else {
            var arr = url.split('&');
            for (var i = 0; i < arr.length; i++) {
                keys.push(arr[i].split('=')[0]);
                vals.push(arr[i].split('=')[1]);
            }
        }
        return { keys: keys, vals: vals };
    }

    this.getActionFromParm = function () {
        var pram = this.getParameters();
        if (pram == null) {
            return null;
        }
        var res = "";
        var otherKey = {};
        var num = 0;
        var keys = pram.keys;
        for (var i = 0; i < keys.length; i++) {
            if (keys[i] == "action") {
                res = pram.vals[i];
            }
            otherKey[pram.keys[i]] = pram.vals[i];
        }
        return { action: res, args: otherKey };
    }
}

var IncludeHelper = function () {
    this.containers = [];
    this.include_html = [];

    this.getIncludeHtml = function () {
        var list = $("body div");
        for (var i = 0; i < list.length; i++) {
            if ($(list[i]).attr("include")) {
                this.containers.push($(list[i]));
                this.include_html.push($(list[i]).attr("include"));
            }
        }
    }

    this.appendHtml = function (container, url) {
        this.get(url, null, function (d) {
            $(container).append($(d));
        });
    }

    this.get = function (url, data, callback) {
        $.ajax({
            url: url + "?ran=" + Math.random(),
            data: data,
            success: callback,
            dataType: "text"
        });
    }

    this.init = function () {
        this.getIncludeHtml();
        for (var i = 0; i < this.containers.length; i++) {
            this.appendHtml(this.containers[i], this.include_html[i]);
        }
    }

    this.resetInclude = function (container, url) {
        $(container).html("");
        $(container).attr("include", url);
        this.appendHtml(container, url);
    }
}

function Controller() {
    this._actionArray = new Array();

    this.registerAction = function (action) {
        this._actionArray.push(action);
    }

    //get action by name
    this.getAction = function (name) {
        var action = null;
        for (var i = 0; i < this._actionArray.length; i++) {
            if (this._actionArray[i].getName().toLowerCase() == name.toLowerCase()) {
                action = this._actionArray[i];
                break;
            }
        }
        return action;
    }

    this.doAction = function (args) {
        this.currentAction= args;
        //1. get action by name
        var action = this.getAction(args.action);
        if (action != null) {
            //2. call action do function
            action.do(args);
        }
    }

    this.restoreAction = function (url) {
        var prams = _historyHelper.getActionFromParm(url);
        if (prams == null) {
            return;
        }
        //1.get action name from url
        var actionName = prams.action;
        //2.get args from url
        var args = prams.args;
        //3.get action by name
        var action = this.getAction(actionName);
        //4.call action do function
        action.do(args);
    }
}

function IAction() {
    this.do = function (state) {
    }

    this.getName = function () {
    }
}
